package com.tracking.application.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.followme.module.common.ElementList;
import com.followme.module.common.ElementListAdapter;
import com.tracking.application.DataBase.DbAdapter;
import com.tracking.application.R;
import com.tracking.application.activities.MainApplicationActivity;
import com.tracking.application.activities.TabsActivity;
import com.tracking.application.util.CommonModuleApp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Kamil.O
 * Description: History fragment
 * Date: 20.11.13
 * Time: 01:51
 */
public class HistoryFragment extends Fragment {
    private MainApplicationActivity mainApplicationActivity;
    private List<ElementList> elementLists = new ArrayList<ElementList>();
    private ElementListAdapter elementListAdapter;
    private DbAdapter db;
    private List<Object> listRoutes;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mainApplicationActivity = (MainApplicationActivity) activity;
            db = new DbAdapter(mainApplicationActivity);
        } catch(ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement DataPullingInterface");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.history_fragment, container, false);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        initializeElementsList();
        ListView listView = (ListView)getView().findViewById(R.id.listView);

        elementListAdapter = new ElementListAdapter(elementLists, mainApplicationActivity);
        listView.setAdapter(elementListAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentAdapter, View view, int position, long id) {
                TextView idRoute = (TextView) view.findViewById(R.id.id_route);

                //TODO: jesli jest nagrywanie to nie wolno
                Intent intent = new Intent(mainApplicationActivity, TabsActivity.class);
                intent.putExtra(CommonModuleApp.KEY_ID_ROUTE, Long.valueOf((String) idRoute.getText()));
                intent.putExtra(CommonModuleApp.KEY_GPS_LAT, -1.0);
                intent.putExtra(CommonModuleApp.KEY_GPS_LONG, -1.0);
                intent.putExtra(CommonModuleApp.KEY_HISTORY_TO_MAP, true);
                intent.putExtra(CommonModuleApp.KEY_MAP_MODE, mainApplicationActivity.getMapMode());
                startActivity(intent);

                Log.d(CommonModuleApp.TAG_LOG_CAT, "Click listView history"+idRoute.getText());
            }
        });

        registerForContextMenu(listView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo aInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;

        ElementList elementList =  elementListAdapter.getItem(aInfo.position);

        menu.setHeaderTitle(R.string.options);
        menu.add(1, 1, 1, R.string.delete);
    }

    private void initializeElementsList() {
        elementLists.clear();

        db.open();
        listRoutes = db.getAllRoute();
        db.close();

        for(int i=0; i<listRoutes.size(); i++){
            elementLists.add(new ElementList((String) listRoutes.get(i), (String) listRoutes.get(i+1), (String) listRoutes.get(i+2)));
            if (i == (listRoutes.size()-3)) break;
            i = i + 2;
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo aInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        String idRoute = elementLists.get(aInfo.position).getIdRoute();

        db.open();
        db.deleteRoad(idRoute);
        db.close();

        initializeElementsList();
        return true;
    }
}
